﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("No username entered!");
                txtName.Focus();
            }
            else if (txtPassword.Text == "")
            {
                MessageBox.Show("No password entered!");
                txtName.Focus();
            }
            else
            {
                try
                {
                    Person login = Person.login(txtName.Text, txtPassword.Text);

                    if (login.PERID == -1)
                    {
                        MessageBox.Show("Incorrect username or password!");
                    }
                    else
                    {
                        this.Hide();

                        if (login.IsManager)
                        {
                            frmManagerMain managerMain = new frmManagerMain(login, this);
                            managerMain.Show();
                        }
                        else
                        {
                            frmNurseMain nurseMain = new frmNurseMain(login, this);
                            nurseMain.Show();
                        }
                    }
                }
                catch (Exception exception) {  // the database connection might throw an error, and it will be passed up to here
                    UTIL.sayDebug(exception);
                    MessageBox.Show("Error: " + exception.Message);
                    
                }
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtName, true, true, false, false, null);
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtPassword, true, true, false, false, null);
        }
    }
}
